{% extends 'base.html' %}

{% block title %}创建许可{% endblock %}

{% block body %}
    <form class="layui-form layui-form-pane" action="">
        {% csrf_token %}
        <div class="layui-form-item">
            <label class="layui-form-label">许可名称</label>
            <div class="layui-input-inline">
                <input type="text" name="name" required lay-verify="required" placeholder="输入许可名"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">url地址</label>
            <div class="layui-input-inline">
                <input type="text" name="url" required lay-verify="text" placeholder="例子：/index/"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">复选框</label>
            <div class="layui-input-block">
                <input type="checkbox" checked="" name="method[GET]" title="GET">
                <input type="checkbox" name="method[POST]" title="POST">
                <input type="checkbox" name="method[DELETE]" title="DELETE">
                <input type="checkbox" name="method[PUT]" title="PUT">
                <input type="checkbox" name="method[PATCH]" title="PATCH">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否为菜单栏</label>
            <div class="layui-input-block">
                <input type="checkbox" name="is_menu" lay-skin="switch" lay-filter="switchTest"
                       lay-text="是|否">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">父级菜单ID</label>
            <div class="layui-input-inline">
                <select name="parent_id" lay-search="">
                    <option value="">直接选择或搜索选择</option>
                    {{ permissions }}
                    {% for permission in permissions %}
                        {% if permission.is_menu and permission.parent_id is null %}
                            <option value={{ permission.id }}>{{ permission.name }}</option>
                        {% endif %}
                    {% endfor %}
                </select>
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 1%;">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="createPermission">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
{% endblock %}
{% block js %}
    <script>
        layui.use(['jquery', 'form', 'layer'], function () {
            var form = layui.form
                , $ = layui.jquery
                , layer = layui.layer;

            form.on('submit(createPermission)', function (data) {
                data = data.field;
                data['csrfmiddlewaretoken'] = $('[name="csrfmiddlewaretoken"]').val();
                $.ajax({
                    url: "{% url 'auth:permission_api' %}",
                    type: 'POST',
                    data: data,
                    dataType: 'json',
                    success: function (res) {
                        if (res.code === 0) {
                            layer.msg(res.msg, {icon: 6});
                            document.location.href = "{% url 'auth:permission' %}", true;
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    },
                    error: function (error) {
                        layer.msg(error.msg, {icon: 5});
                    }
                })
                return false;
            });

        });
    </script>
{% endblock %}